package com.hmall.order.handle;
import com.hmall.order.utils.UserHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler) throws Exception {
        //1-获取请求头
        String header = request.getHeader("authorization");
        if (StringUtils.isBlank(header)){
            throw new RuntimeException("用户未登录，请先登录！");
        }
        //2-将字符串类型转为Long
        Long userId = Long.valueOf(header);
        //3-将userId存放到ThreadLocal
        UserHolder.init(userId);

        return true;
    }

    @Override
    public void afterCompletion(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, Exception ex) throws Exception {
     UserHolder.remove();
    }
}
